﻿using System.IO;
using HappyBa.Core;
using HappyBa.Core.Interfaces;
using HappyBa.Core.Reporting;
using HappyBa.Localization;
using LightInject;
using Mnk.Library.CodePlex;
using Mnk.Library.Common.AutoUpdate;
using Mnk.Library.Common.Log;
using Mnk.Library.Common.SaveLoad;
using Mnk.Library.WpfControls;
using Mnk.Library.WpfControls.Code.Log;

namespace HappyBa.Code
{
    static class ServicesRegistrar
    {
        private static readonly string ErrorsLogsPath = Path.Combine(Folders.UserLogsFolder, "HappyBa.Errors.log");

        static ServicesRegistrar()
        {
            LogManager.Init(
                new MultiplyLog(
                    new FileLog(ErrorsLogsPath),
                    new MessageBoxLog()),
                new FileLog(Path.Combine(Folders.UserLogsFolder, "HappyBa.Info.log"))
                );
        }

        public static IServiceContainer Register(ConfigurationSerializer<Config> serializer)
        {
            var container = new ServiceContainer();
            container.RegisterInstance(serializer.Load(new Config()));
            Core.ServicesRegistrar.Register(container);

            container.Register<IDataCollector, DataCollector>(new PerContainerLifetime());
            container.Register<IReportFilePathGetter, ReportFilePathGetter>(new PerContainerLifetime());

            container.RegisterInstance<IApplicationUpdater>(new CodePlexApplicationUpdater("HappyBa"));
            container.Register<IAutoUpdater, ApplicationUpdater>(new PerContainerLifetime());
            container.Register<IThemesManager, ThemesManager>(new PerContainerLifetime());
            container.RegisterInstance<IFeedbackSender>(new FeedbackSender(HappyBaLang.Caption, "http://mnk92.cloudapp.net:61234"));
            container.RegisterInstance<ILogsSender>(new LogsSender(ErrorsLogsPath, container.GetInstance<IFeedbackSender>()));

            return container;
        }
    }
}
